/*
 *  ArduinoOnXcode
 *
 *  Created by Rick Miley on 3/17/11.
 *  Copyright 2011 __MyCompanyName__. All rights reserved.
 *
 */


#include <ME218_CLUT.h>

// ==================== start_LED_sprite ===================

int	current_LED_sprite=0;

void start_LED_sprite(int choose_LED_sprite) {
	live_LED=choose_LED_sprite;
	LED_Table_index=0;
	LED_Table_delay=0;
}
	

// ==================== write_out_LED_value ===================

void write_out_LED_value(unsigned char mask) {
	int i,flip;
	
	LED_Table_delay=-1;		// fixed LED clut
	LED_Table_index=0;
	live_LED=0;
	
	
	// brute force
	flip=0;
	if(mask & 0x80)
		flip|=0x01;
	
	if(mask & 0x40)
		flip|=0x02;
	
	if(mask & 0x20)
		flip|=0x04;
	
	if(mask & 0x10)
		flip|=0x08;
	
	if(mask & 0x08)
		flip|=0x10;
	
	if(mask & 0x04)
		flip|=0x20;
	
	if(mask & 0x02)
		flip|=0x40;
	
	if(mask & 0x01)
		flip|=0x80;
	
			
	for(i=0;i<16;i++) {
		if(default_LED_brightness>i)
			LED_brightness_table[i]=flip;
		else
			LED_brightness_table[i]=0;

	}
}

// =================== set_LED_brightness ===================

void set_LED_brightness(unsigned char intensity) {
	default_LED_brightness=intensity;
}

// ================== Initialize LEDs =====================


void initializeLEDs() { 
	int i;
	
//	set_LED_brightness(0X01);
	BRIGHT_LIGHT_0x80=0;
	BRIGHT_LIGHT_0x40=0;
	BRIGHT_LIGHT_0x20=0;
	BRIGHT_LIGHT_0x10=0;
	BRIGHT_LIGHT_0x08=0;
	BRIGHT_LIGHT_0x04=0;
	BRIGHT_LIGHT_0x02=0;
	BRIGHT_LIGHT_0x01=0;
		
	GREMLIN=0;
	
}



// ================== Initialize CLUTs =====================


void initializeCLUTs() { 	
	
	
	
	int red_temp, green_temp, blue_temp, delay_temp;
	
	CLUT_Sprite_Sizes  *qq;
	CLUT_FLASH_data *temp_CT;
	//	CLUT_FLASH_data*  tt;
	
	int i,j;
	
	
	// Note: The CLUT_mapper is larger than the ColorTable.
	// A large CLUT_mapper allows every Graphic Sprite
	// to hold a unique mask value. As more hardware
	// capabilites are developed, richer content can
	// be exposed without significantly changing source code.
	
	// With 16 CLUTs the CLUT_mapper contents repeat 
	for (j=0;j<8;j++) {
		for (i=0;i<16;i++) 
			CLUT_mapper[16*j+i]=i;	
	}
	
	
	for (i=0;i<numCLUTs;i++) {
		//		temp_CT=CLUT_Sequence[i].the_sprite_array;
		//		temp_index=CLUT_Sequence[i].the_sprite_len;
		qq=(CLUT_Sprite_Sizes*)SUPER_MAGIC_CLUT_VARIABLE;
		qq+=i;
		
		
		temp_CT=(CLUT_FLASH_data*)pgm_read_word_near(&qq->the_sprite_array);
		//		temp_index=pgm_read_word_near(&qq->the_sprite_len);
		
		
		// load with these new parameters
		delay_temp = pgm_read_word_near(&temp_CT->cs_delay);
		red_temp = pgm_read_word_near(&temp_CT->cs_red);
		green_temp = pgm_read_word_near(&temp_CT->cs_green);
		blue_temp = pgm_read_word_near(&temp_CT->cs_blue);
		
//		ColorTable[i].lc_delay=delay_temp;        
//		ColorTable[i].lc_index=0;    
		
		ColorTable_delay[i]=delay_temp;
		ColorTable_index[i]=0;
		
		ColorTable[i].live_red_value= red_temp;	
		ColorTable[i].live_green_value= green_temp;
		ColorTable[i].live_blue_value= blue_temp;
	}
	
	
}


